#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
import sys

# 读取两个文件（相对于项目根目录）
with open('../output/dhf_output_chujinggang.json', 'r', encoding='utf-8') as f:
    chujinggang = json.load(f)

with open('../output/dhf_output.json', 'r', encoding='utf-8') as f:
    dhf = json.load(f)

qSim_cjg = chujinggang['qSim']
runoffSim_cjg = chujinggang['runoffSim']
timeSeries_dhf = dhf['timeSeries']

print("=" * 120)
print("QSim 时间序列对比 (前30个时段)")
print("=" * 120)
print(f"{'索引':<6} {'时间':<20} {'QSim(初境港)':<20} {'QSim(DHF)':<20} {'差值':<15} {'一致性':<10}")
print("-" * 120)

qsim_match_count = 0
qsim_total = min(30, len(qSim_cjg), len(timeSeries_dhf))

for i in range(qsim_total):
    qsim_cjg = qSim_cjg[i]
    qsim_dhf = timeSeries_dhf[i]['QSim']
    time_dhf = timeSeries_dhf[i]['time']
    is_warmup = timeSeries_dhf[i].get('isWarmUp', False)
    
    diff = abs(qsim_cjg - qsim_dhf)
    is_match = diff < 1e-6
    
    if is_match:
        qsim_match_count += 1
    
    warmup_mark = " [WarmUp]" if is_warmup else ""
    match_mark = "✓" if is_match else "✗"
    
    print(f"{i:<6} {time_dhf:<20} {qsim_cjg:<20.6f} {qsim_dhf:<20.6f} {diff:<15.9f} {match_mark:<10}{warmup_mark}")

print(f"\nQSim 一致性: {qsim_match_count}/{qsim_total} ({qsim_match_count*100/qsim_total:.1f}%)")

print("\n" + "=" * 120)
print("runoffSim 时间序列对比 (前30个时段)")
print("=" * 120)
print(f"{'索引':<6} {'时间':<20} {'runoffSim(初境港)':<20} {'runoffSim(DHF)':<20} {'差值':<15} {'一致性':<10}")
print("-" * 120)

runoff_match_count = 0
runoff_total = min(30, len(runoffSim_cjg), len(timeSeries_dhf))

for i in range(runoff_total):
    rs_cjg = runoffSim_cjg[i]
    rs_dhf = timeSeries_dhf[i]['runoffSim']
    time_dhf = timeSeries_dhf[i]['time']
    is_warmup = timeSeries_dhf[i].get('isWarmUp', False)
    
    diff = abs(rs_cjg - rs_dhf)
    is_match = diff < 1e-6
    
    if is_match:
        runoff_match_count += 1
    
    warmup_mark = " [WarmUp]" if is_warmup else ""
    match_mark = "✓" if is_match else "✗"
    
    print(f"{i:<6} {time_dhf:<20} {rs_cjg:<20.6f} {rs_dhf:<20.6f} {diff:<15.9f} {match_mark:<10}{warmup_mark}")

print(f"\nrunoffSim 一致性: {runoff_match_count}/{runoff_total} ({runoff_match_count*100/runoff_total:.1f}%)")

# 全部时段统计
print("\n" + "=" * 120)
print("全部时段统计")
print("=" * 120)

total_len = min(len(qSim_cjg), len(timeSeries_dhf))
qsim_all_match = sum(1 for i in range(total_len) if abs(qSim_cjg[i] - timeSeries_dhf[i]['QSim']) < 1e-6)
runoff_all_match = sum(1 for i in range(total_len) if abs(runoffSim_cjg[i] - timeSeries_dhf[i]['runoffSim']) < 1e-6)

print(f"总时段数: {total_len}")
print(f"QSim 完全一致的时段数: {qsim_all_match}/{total_len} ({qsim_all_match*100/total_len:.1f}%)")
print(f"runoffSim 完全一致的时段数: {runoff_all_match}/{total_len} ({runoff_all_match*100/total_len:.1f}%)")

# 最终状态对比
print("\n" + "=" * 120)
print("最终状态对比")
print("=" * 120)

sa0_cjg_last = float(chujinggang['newStatus']['SA0'][-1])
ua0_cjg_last = float(chujinggang['newStatus']['UA0'][-1])
ya0_cjg_last = float(chujinggang['newStatus']['YA0'][-1])

sa0_dhf = dhf['finalState']['SA0']
ua0_dhf = dhf['finalState']['UA0']
ya0_dhf = dhf['finalState']['YA0']

sa0_match = abs(sa0_cjg_last - sa0_dhf) < 1e-6
ua0_match = abs(ua0_cjg_last - ua0_dhf) < 1e-6
ya0_match = abs(ya0_cjg_last - ya0_dhf) < 1e-6

print(f"SA0: 初境港={sa0_cjg_last:.10f}, DHF={sa0_dhf:.10f}, 差值={abs(sa0_cjg_last - sa0_dhf):.10f} {'✓' if sa0_match else '✗'}")
print(f"UA0: 初境港={ua0_cjg_last:.10f}, DHF={ua0_dhf:.10f}, 差值={abs(ua0_cjg_last - ua0_dhf):.10f} {'✓' if ua0_match else '✗'}")
print(f"YA0: 初境港={ya0_cjg_last:.10f}, DHF={ya0_dhf:.10f}, 差值={abs(ya0_cjg_last - ya0_dhf):.10f} {'✓' if ya0_match else '✗'}")

# 总体结论
print("\n" + "=" * 120)
print("总体结论")
print("=" * 120)

if qsim_all_match == total_len and runoff_all_match == total_len and sa0_match and ua0_match and ya0_match:
    print("✓✓✓ 两个模型输出**完全一致**！")
    sys.exit(0)
elif qsim_all_match >= total_len * 0.95 and runoff_all_match >= total_len * 0.95:
    print("⚠ 两个模型输出基本一致，但存在少量差异")
    sys.exit(0)
else:
    print("✗✗✗ 两个模型输出存在显著差异，需要进一步检查")
    
    # 找出第一个不匹配的时段
    first_diff_qsim = next((i for i in range(total_len) if abs(qSim_cjg[i] - timeSeries_dhf[i]['QSim']) >= 1e-6), -1)
    first_diff_runoff = next((i for i in range(total_len) if abs(runoffSim_cjg[i] - timeSeries_dhf[i]['runoffSim']) >= 1e-6), -1)
    
    if first_diff_qsim >= 0:
        print(f"\n第一个 QSim 不匹配出现在索引 {first_diff_qsim}")
        print(f"  时间: {timeSeries_dhf[first_diff_qsim]['time']}")
        print(f"  初境港: {qSim_cjg[first_diff_qsim]:.10f}")
        print(f"  DHF:    {timeSeries_dhf[first_diff_qsim]['QSim']:.10f}")
        print(f"  差值:   {abs(qSim_cjg[first_diff_qsim] - timeSeries_dhf[first_diff_qsim]['QSim']):.10f}")
    
    if first_diff_runoff >= 0:
        print(f"\n第一个 runoffSim 不匹配出现在索引 {first_diff_runoff}")
        print(f"  时间: {timeSeries_dhf[first_diff_runoff]['time']}")
        print(f"  初境港: {runoffSim_cjg[first_diff_runoff]:.10f}")
        print(f"  DHF:    {timeSeries_dhf[first_diff_runoff]['runoffSim']:.10f}")
        print(f"  差值:   {abs(runoffSim_cjg[first_diff_runoff] - timeSeries_dhf[first_diff_runoff]['runoffSim']):.10f}")
    
    sys.exit(1)

